package com.service.impl;

import com.dao.*;
import com.entity.*;
import com.service.inte.PartyOrgBranchServiceInte;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PartyOrgBranchServiceImpl implements PartyOrgBranchServiceInte {

    @Autowired
    private PartyOrgBranchDao partyOrgBranchDao;

    @Autowired
    private Td_tblPartyOrgDetailDao td_tblPartyOrgDetailDao;

    @Autowired
    private Td_tblPartyOrgbranchApproveDao partyOrgbranchApproveDao;

    @Autowired
    private Td_tblPartyOrgBranchPunishDao partyOrgBranchPunishDao;

    @Autowired
    private TD_tblPartychangelogDao partychangelogDao;

    @Override
    public List<Td_tblPartyOrgBranch> getAllBrnch(){
        return partyOrgBranchDao.getAllBrnch();
    }

    @Override
    public int addMember(int branchId){
        return partyOrgBranchDao.addMember(branchId);
    }

    @Override
    public List<Object[]> getNewBranchByParent(int parentBranchId,String branchName, int page) {
        return partyOrgBranchDao.getNewBranchByParent(parentBranchId,"%"+branchName+"%", new PageRequest(page,10));
    }

    @Override
    public List<Td_tblPartyOrgBranch> getNewBranch(int page) {
        return partyOrgBranchDao.getNewBranch(new PageRequest(page,10));
    }

    @Override
    public int agreeSetUp(String userName, int branchId) {
        return partyOrgBranchDao.agreeSetUp(userName,branchId);
    }

    @Override
    public List<Td_tblPartyOrgBranch> getOppisitionSetUp(int branchId,int page) {
        return partyOrgBranchDao.getOppisitionSetUp(branchId,new PageRequest(page,10));
    }

    @Override
    public List<Object[]> getByParent(int branchId, String branchName, int page) {
        return partyOrgBranchDao.getByParent(branchId,branchId,"%"+branchName+"%",new PageRequest(page,10));
    }

    @Override
    public Td_tblPartyOrgBranch getBybranchId(int branchId) {
        return partyOrgBranchDao.getBybranchId(branchId);
    }

    @Override
    public Td_tblPartyOrgDetail getSecretaryByBranch(int branchId) {
        return td_tblPartyOrgDetailDao.getSecretaryByBranch(branchId);
    }

    @Override
    @Transactional
    public int saveOrSaveApprove(Td_tblPartyOrgbranchApprove td_tblPartyOrgbranchApprove) {
        try {
            partyOrgbranchApproveDao.save(td_tblPartyOrgbranchApprove);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    @Override
    public int saveOrUpdateBranch(Td_tblPartyOrgBranch td_tblPartyOrgBranch) {
        try {
            partyOrgBranchDao.save(td_tblPartyOrgBranch);
            return 1;
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }
    }

    @Override
    public List<Object[]> getBeBranchByFd_branchId(int branchId,String branchName, int page) {
        return partyOrgbranchApproveDao.getBeBranchByFd_branchId(branchId,"%"+branchName+"%",new PageRequest(page,10));
    }

    @Override
    public List<Object[]> getByLoginId(int loginId, String branchName, int page) {
        return partyOrgBranchPunishDao.getByLogin(loginId,"%"+branchName+"%",new PageRequest(page,10));
    }

    @Override
    public Td_tblPartyOrgBranchPunish getByFd_id(int id) {
        return partyOrgBranchPunishDao.getByFd_id(id);
    }

    @Override
    public int saveOrUpdateBranchPunish(Td_tblPartyOrgBranchPunish td_tblPartyOrgBranchPunish) {
        try {
            partyOrgBranchPunishDao.save(td_tblPartyOrgBranchPunish);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public List<Object[]> getByParentId(int parentId, String branchName, int page) {
        return partyOrgBranchDao.getByParentId(parentId,"%"+branchName+"%",new PageRequest(page,10));
    }

    @Override
    public List<Object[]> getPunishByLoginId(int loginId, String branchName, int page) {
        return partyOrgBranchPunishDao.getByLogin(loginId,"%"+branchName+"%",new PageRequest(page,10));
    }

    @Override
    public Td_tblPartyOrgBranchPunish getPunishById(int punishId) {
        return partyOrgBranchPunishDao.getByFd_id(punishId);
    }

    @Override
    public List<String> getAllLeaderByBranchId(int branchId) {
        List<Object[]> allByBranchId = td_tblPartyOrgDetailDao.getAllByBranchId(branchId);
        List<String> leaders=new ArrayList<>();
        leaders.add("");
        leaders.add("");
        leaders.add("");
        for(int i=0;i<allByBranchId.size();i++){
            switch (allByBranchId.get(i)[0].toString()){
                case "党支部书记":
                    leaders.set(0,allByBranchId.get(i)[1].toString());
                    break;
                case "党支部副书记":
                    leaders.set(1,leaders.get(1)+allByBranchId.get(i)[1].toString()+",");
                    break;
                case "党支部委员":
                    leaders.set(2,leaders.get(2)+allByBranchId.get(i)[1].toString()+",");
                    break;
            }
        }
        for(int i=1;i<leaders.size();i++){
            if (leaders.get(i).length()>0){
                leaders.set(i,leaders.get(i).substring(0,leaders.get(i).length()-1));
            }
        }
        return leaders;
    }

    @Override
    public String[] getLastChangeLeader(int branchId) {
        Object[] objects = partychangelogDao.lastChangeLeader(branchId).get(0);
        return new String[]{objects[0].toString(),objects[1].toString()};
    }

    @Override
    @Transactional
    public void saveOrUploadBranchChangeLog(TD_tblPartychangelog partychangelog) {
        partychangelogDao.save(partychangelog);
    }
}

